/*
 * vip 中用到的配置选项，追加在免费邮箱的配置选项之后（是在免费的基础上对其进行了方法的添加和事件的添加）
 */

(function() {
	if(!mailCfgClass) return false;
	mailCfgClass.addMethod('vipSpecialArea', function(rsh) {
		hiddenAllPanel();
		DOM.mailConfiguration.innerHTML = vipAreaTemplate;
		DOM.mailConfiguration.show();
		addAjaxHistory('vipSpecialArea', 'changePanel("mailConfiguration", "vipSpecialArea", true)', mailCfgClass.getCfgNameByCfgId('vipSpecialArea'), rsh);
	}, '贵宾专区');
	mailCfgClass.addMethod('translateDemo', function(rsh) {
		if(frozenAlert())
			return;
		hiddenAllPanel();
		DOM.mailConfiguration.innerHTML = translateOnlineTemplate;
		DOM.mailConfiguration.show();
		addAjaxHistory('translateDemo', 'changePanel("mailConfiguration", "translateDemo", true)', mailCfgClass.getCfgNameByCfgId('translateDemo'), rsh);
	}, '在线翻译');
	mailCfgClass.addMethod('stockDemo', function(rsh) {
		if(frozenAlert())
			return;
		hiddenAllPanel();
		DOM.mailConfiguration.innerHTML = stockTemplate;
		DOM.mailConfiguration.show();
		var o = document.getElementsByName("radiobutton");
		if(PROFILE.feedstock==0 && o){
			o[1].checked=true;
		}
		addAjaxHistory('stockDemo', 'changePanel("mailConfiguration", "stockDemo", true)', mailCfgClass.getCfgNameByCfgId('stockDemo'), rsh);
	}, '股票时时通');
	mailCfgClass.addMethod('mapDemo', function(rsh) {
		if(frozenAlert())
			return;
		hiddenAllPanel();
		DOM.mailConfiguration.innerHTML = mapOnlineTemplate;
		DOM.mailConfiguration.show();
		addAjaxHistory('stockDemo', 'changePanel("mailConfiguration", "mapDemo", true)', mailCfgClass.getCfgNameByCfgId('mapDemo'), rsh);
	}, '在线地图');

	mailCfgClass.addMethod('cardSignDemo', function(rsh) {
		if(frozenAlert())
			return;
		CardSign.showPanel();
		addAjaxHistory('cardSign', 'changePanel("mailConfiguration", "cardSignDemo", true)', mailCfgClass.getCfgNameByCfgId('cardSignDemo'), rsh);
	}, '名片签名');
	mailCfgClass.addMethod('businessMailDemo', function(rsh) {
		jsoplog.ucCapture('weml_write' );
		if(frozenAlert())
			return;
		businessmail.init();
		addAjaxHistory('businessMail', 'changePanel("mailConfiguration", "businessMailDemo", true)', mailCfgClass.getCfgNameByCfgId('businessMailDemo'), rsh);
	}, '商信计划');
	// vip中设置过滤级别
	mailCfgClass.addMethod('vipFilterLevel', function(rsh) {
		get_filterlevel({}, function(ret) {
			if(!ret.level) {
				_dialog.alert({info: '系统错误，请稍后重试~'});
				return false;
			}
			var data = {};
			switch(ret.level) {
				case '1':
					data['lowlevel'] = 'checked';
					break;
				case '2':
					data['middlelevel'] = 'checked';
					break;
				case '3':
					data['highlevel'] = 'checked';
					break;
				default:
					break;
			};
			DOM.mailConfiguration.innerHTML = new Template(mailSecurityLevelTemplate).evaluate(data);
		}, function() {
			_dialog.alert({info: '系统错误，请稍后重试~'});
			return false;
		});
	});
	mailCfgClass.addMethod('setFilterLevel', function() {
		var c1 = $('setantispam1').checked, c2 = $('setantispam2').checked, c3 = $('setantispam3').checked,
		l = c1 ? '1' : c2 ? '2' : c3 ? '3' : '0',
		params = {level: l};
		set_filterlevel(params, function(ret) {
			if(ret.is_success) {
				changePanel('mailConfiguration', 'initMailCfg');
			} else {
				_dialog.alert({info: '系统错误，请稍后重试~'});
				return false;
			}
		}, function() {
			_dialog.alert({info: '系统错误，请稍后重试~'});
			return false;
		});
	});
	// vip杀毒功能设置的页面
	mailCfgClass.addMethod('vipCleanVirus', function() {
		DOM.mailConfiguration.innerHTML = mailAntivirusTemplate;
	});
	mailCfgClass.addDlEvent([
		{name: 's_rt_inbox', exec: function() {
			changePanel('mailList', 1);
		}},
		{name: 's_use_shop', exec: function() {
			toJoinActivity2('vip_7shop24');
		}},
		{name: 's_china_pub', exec: function() {
			toJoinActivity('vip_chinapub');
		}},
		{name: 's_golden_holiday', exec: function() {
			toJoinActivity2('vip_goldenHoliday');
		}},
		{name: 's_rt_business', exec: function() {
			changePanel('mailConfiguration', 'vipBusinessArea');
		}},
		{name: 's_translate_demo', exec: function() {
			changePanel('mailConfiguration', 'translateDemo');
		}},
		{name: 's_stock_demo', exec: function() {
			changePanel('mailConfiguration', 'stockDemo');
		}},
		{name: 's_pop_demo', exec: function() {
			mailPop.getPop3List();
			var _b = document.createElement('b');
			_b.innerHTML = '[<a class="grey s_rt_business" href="javascript:;">返回商务通首页</a>]';
			DOM.mailConfiguration.getElementsByTagName('h4')[0].appendChild(_b);
		}},
		{name: 's_map_demo', exec: function() {
			changePanel('mailConfiguration', 'mapDemo');
		}},
		{name: 's_custom_demo', exec: function() {
			changePanel('mailConfiguration', 'businessMailDemo');
		}},
		{name: 's_sign_demo', exec: function() {
			changePanel('mailConfiguration', 'cardSignDemo');
		}},
		{name: 's_card_sign', exec: function(e) {
			var cds = $('cardsign'), cms = $('commonsign');
			if(!cds.visible()) {
				cds.show();
				cms.hide();
			}
			e.checked = true;
		}},
		{name: 's_common_sign', exec: function(e) {
			var cds = $('cardsign'), cms = $('commonsign');
			if(!cms.visible()) {
				cds.hide();
				cms.show();
			}
			e.checked = true;
		}},
		{name: 's_vip_sign_preview', exec: function() {
			CardSign.preview();
		}},
		{name: 's_vip_set_sign', exec: function() {
			var params = {};
			params.image_signature = $('cfg_image_signature').checked;
			if($('radioTwo').checked) {
				params.signatureCommon = Editor1.getContent();
				params.signtype = 1;
			} else {
				var obj = {}, arr = ['username', 'userdepartment', 'usercompany', 'userwebpage', 'userbusiness_address', 'userpostalcode', 'userphone', 'usermobile', 'userfax', 'useremail', 'usermsn', 'userqq'];
				$A($('cardsign').getElementsByTagName('input')).each(function(item, index) {
					obj[arr[index]] = item.value;
				});
				params.signatureCard = Object.toJSON(obj);
				params.signtype = 0;
			}
			profile_set(params, function(ret) {
				if(ret.is_success) {
					changePanel('mailConfiguration', 'vipBusinessArea');
				} else {
					_dialog.alert({info: '系统出错了，请稍后重试'});
				}
			}, function() {
				_dialog.alert({info: '系统出错了，请稍后重试'});
			});
		}},
		//
		{name: 's_group_input', exec:function(EL) {
			var _name = EL.gname ? EL.gname : EL.getAttribute('gname');
			inputGroupMail(_name);
		}},
		{name: 's_show_group', exec: function(EL) {
			var _id = EL.gid ? EL.gid : EL.getAttribute('gid');
			isShowAddrGroup(EL, _id);
		}},
		{name: 's_address_input', exec: function(EL) {
			var _name = EL.cname ? EL.cname : EL.getAttribute('cname'),
			_email = EL.cemail ? EL.cemail : EL.getAttribute('cemail');				
			copyAddressBook(_name, _email);
			addressbook_use_flag = 1;
		}},
		{name: 's_cfg_filterlevel', exec: function() {
			changePanel('mailConfiguration', 'vipFilterLevel');
		}},
		{name: 's_set_filterlevel', exec: function() {
			changePanel('mailConfiguration', 'setFilterLevel');
		}},
		{name: 's_vip_cleanvirus', exec: function() {
			changePanel('mailConfiguration', 'vipCleanVirus');
		}}
	]);
	
	mailCfgClass.addMethod('vipBusinessArea', function(rsh) {
		hiddenAllPanel();
		DOM.mailConfiguration.innerHTML = vipBusinessTemplate;
		DOM.mailConfiguration.show();
		addAjaxHistory('vipBusinessArea', 'changePanel("mailConfiguration", "vipBusinessArea", true)', mailCfgClass.getCfgNameByCfgId('vipBusinessArea'), rsh);
		ExecFunc(frozenAlert);
	}, '商务通');
})();

// (AJAX交互部分)
// vip活动chinapub
function toJoinActivity(active) {
	if (G.userinfo.user_ext_status != '1') {
		_dialog.alert({	info: '对不起，您不是正常期用户，请续费后再来申请！'});
		return false;
	} 
	try {
		if (typeof chinapubResult == 'undefined') {
			if (active == 'vip_chinapub') {
				var params = {
					email: UID,
					status: 'add'
				};
				var url = 'http://vip.sohu.com/servlet/ChinaPub?' + $H(params).toQueryString();
				loadScript(url, function(){
					chinaPubCallBack(chinaPubResult.ret, chinaPubResult.ret1);
				});
			}
		} else {
			chinaPubCallBack(chinapubResult, chinapubResult1);
		}
	} catch(e) {}
}

//vip活动黄金假日
function toJoinActivity2(active) {
	if (G.userinfo.user_ext_status != '1') {
		_dialog.alert({	info: '对不起，您不是正常期用户，请续费后再来申请！'});
		return false;
	} 
	try {
		if (active == 'vip_goldenHoliday') {
			window.open('http://vip.sohu.com/memberActive/validateMember.jsp?app=goldenHoliday');
		} else if (active == 'vip_7shop24') {
			window.open("http://vip.sohu.com/memberActive/validateMember.jsp?app=7shop24");
		}
	} catch(e) {}
}

function chinaPubCallBack() {
	try {
		chinapubResult = arguments[0];
		chinapubResult1 = arguments[1];
		var _list = [{
			type: '-1,-3,-4',
			info: '您请求一个非法连接！'
		}, {
			type: '0',
			info: '对不起，您正在试用期，请续费后再来申请！'
		}, {
			type: '2,3',
			info: '对不起，您正在续费期，请续费后再来申请！'
		}, {
			type: '4',
			info: '对不起，您正在冻结期，请续费后再来申请！'
		}, {
			type: '6',
			info: '对不起，您正在试用-冻结期，请续费后再来申请！'
		}, {
			type: '5',
			info: '您已是china-pub贵宾会员，只需登陆china-pub即可享受五星级购书待遇。如需长期使用，请尽快给VIP邮箱续费！',
			exec: function() {
				window.open(chinapubResult1);
			}
		}], _alert = false;
		_list.each(function(item){
			if (item.type.include(chinapubResult)) {
				_dialog.alert({
					info: item.info
				});
				if (item.exec && typeof item.exec == 'function') {
					item.exec(chinapubResult1);
				}
				_alert = true;
			}
		});
		if (!_alert) {
			window.open(chinapubResult);
		}
	} catch(e) {}
}

var CardSign = {
	get_profile_content:PROFILE,
	showPanel:function(){
		profile_get({}, function(ret) {
			try {
				this.initPanel(ret);
			} catch(e) {}
		}.bind(this));
	},
	initPanel:function(ret){
		mailCfgClass.get_profile_content = ret;
		var t = new Template(cardSignTemplate);
		var data = {};
		if (mailCfgClass.get_profile_content.image_signature) {
			data['image_signature'] = '<input type="checkbox" checked name="cfg_image_signature" id="cfg_image_signature" value="true" />';
		} else {
			data['image_signature'] = '<input type="checkbox" name="cfg_image_signature" id="cfg_image_signature" value="false" />';
		}
		
		if(mailCfgClass.get_profile_content.image_signature_name){
			data['image_path'] = SIGN_ROOT + '/' + mailCfgClass.get_profile_content.image_signature_name + '.jpg?' + Math.random();
		}else{
			data['image_path'] = MailConst.IMG + '/demo1.jpg';
		}

       if(mailCfgClass.get_profile_content.signatureCard){
			var cardsignature = mailCfgClass.get_profile_content.signatureCard.evalJSON();
			data['username']=cardsignature.username;
			data['userdepartment']=cardsignature.userdepartment;
			data['usercompany']=cardsignature.usercompany;
			data['userwebpage']=cardsignature.userwebpage;
			data['userbusiness_address']=cardsignature.userbusiness_address;
			data['userpostalcode']=cardsignature.userpostalcode;
			data['userphone']=cardsignature.userphone;
			data['usermobile']=cardsignature.usermobile;
			data['userfax']=cardsignature.userfax;
			data['useremail']=cardsignature.useremail;
			data['usermsn']=cardsignature.usermsn;
			data['userqq']=cardsignature.userqq;
		} else {
			data['username']="";
			data['userdepartment']="";
			data['usercompany']="";
			data['userwebpage']="";
			data['userbusiness_address']="";
			data['userpostalcode']="";
			data['userphone']="";
			data['usermobile']="";
			data['userfax']="";
			data['useremail']="";
			data['usermsn']="";
			data['userqq']="";
		}
		DOM.mailConfiguration.innerHTML = t.evaluate(data);
		
		if(mailCfgClass.get_profile_content.signtype==0) {
			if (!mailCfgClass.get_profile_content.signatureCard) {
				mailCfgClass.get_profile_content.signatureCard = '';
			}
			if (!mailCfgClass.get_profile_content.signatureCommon) {
				mailCfgClass.get_profile_content.signatureCommon = '';
			}
			$('cardsign').show();
		    $('commonsign').hide();
			$('radioTwo').checked = false;
			$('radioOne').checked = true;
		} else if(mailCfgClass.get_profile_content.signtype==1) {
			if (!mailCfgClass.get_profile_content.signatureCard) {
				mailCfgClass.get_profile_content.signatureCard = '';
			}
			if (!mailCfgClass.get_profile_content.signatureCommon) {
				mailCfgClass.get_profile_content.signatureCommon = '';
			}
			$('cardsign').hide();
		    $('commonsign').show();
			$('radioTwo').checked = true;
			$('radioOne').checked = false;
		} else {
			if (!mailCfgClass.get_profile_content.signatureCard) {
				mailCfgClass.get_profile_content.signatureCard = '';
			}
			if (!mailCfgClass.get_profile_content.signatureCommon) {
				mailCfgClass.get_profile_content.signatureCommon = '';
			}
			$('cardsign').show();
		    $('commonsign').hide();
			$('radioTwo').checked = false;
			$('radioOne').checked = true;
		}
		
		Editor1 = new HTMLEditor(['bold', 'italic', 'underline', 'fontselect', 'fontsizeselect', 'forecolor', 'createlink']);
		var __t = setTimeout(function() {
			clearTimeout(__t);
			__t = null;
			Editor1.create('ineditorimage');
		}, 1);
		var _onload = function() {
			var _t = setTimeout(function() {
				clearTimeout(_t);
				_t = null;
				var _c = Editor1.c().document.body.innerHTML;
				try{
					if (_c && _c != '') {
						if(CardSign.get_profile_content.signatureCommon){
							Editor1.setContent(CardSign.get_profile_content.signatureCommon);
						} else {
							Editor1.setContent('<br />');
						}
					} else {
						_onload();
					}
				} catch(e) {
					_onload();
				}
			}, 500);
		};
		_onload();		
	},
	getProfileContent:function(){
		if(typeof(this.get_profile_content)=="string")
			this.get_profile_content = ExecFunc(function(){return this.get_profile_content.evalJSON()});
		return this.get_profile_content;
	},
	preview: function() {
		var _ck = $('cfg_image_signature'), _dm = $('demoimg'), _dm_src = _dm.src, _n_dm = '', _w = 0, _st = $('radioOne').checked, _html = '', _ct = '';
		if(_ck.checked) {
			_n_dm = '<img style="width:'+ _dm.style.width +'" src="'+ _dm_src +'" />';
			_w = '180px';
		}
		if(_st) {
			var c = ['姓　　名：', '部　　门：', '公　　司：', '网　　址：', '办公地址：', '邮政编码：', '电　　话：', '手　　机：', '电子邮件：', '传　　真：', 'MSN　 &nbsp;：', 'QQ　　  ：'],
			ca = [];
			$A($('cardsign').getElementsByTagName('input')).each(function(item, index) {
				ca.push('<div style="height:24px;"><div class="cfg-sign-txt" style="margin-right:10px;">'+ c[index] +'</div><div style="float:left;">'+ item.value +'</div></div>');
			});
			_ct = ca.join('');
		} else {
			_ct = Editor1.getContent();
		}
		_html = '<div style="margin:0 auto;width:570px;background-color:white;height:430px;">'
			+'<div style="height:340px;overflow-y:auto;padding:30px;">'
			+'<table cellspacing=0 cellpadding=0 style="border:none;"><tbody><tr><td style="width:'+ _w +'" valign="top">'+ _n_dm +'</td><td valign="top">'+ _ct +'</td></tr></tbody></table>'
			+'</div>'
			+'<div class="closebt" style="width:100%;height:30px;"><div style="float:right;width:40px;cursor:pointer;"><img style="border:none;" src="'+ MailConst.ALL +'/close.gif" /></div></div>'
			+'</div>';
		lightBox.show({html: _html});
	}
};